import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VistaAlumno extends javax.swing.JFrame {
	private JPanel jPanel1;
	private JLabel jLabel3;
	private JButton btnRetirar;
	private JTextField txtNombre;
	private JButton btnLimpiar;
	private JButton btnBuscar;
	private JButton btnRegistrar;
	private JTextField txtApellido;
	private JTextField txtCedula;
	private JLabel jLabel2;
	private JLabel jLabel1;

	/**
	* Auto-generated main method to display this JFrame
	*/
	/*public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				VistaAlumno inst = new VistaAlumno();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	*/
	
	public void datosAlumno(Seccion seccion) throws IOException
	{	
		if (txtCedula.getText().equals("")|| txtNombre.getText().equals("")|| txtApellido.getText().equals(""))
		{
			JOptionPane.showMessageDialog(null, "Debe llenar todos los Campos", "Sistema", JOptionPane.INFORMATION_MESSAGE);
		}
		else {		
			if (!existeElem(txtCedula.getText(), seccion)){
				Alumno alumno = new Alumno (txtNombre.getText(), txtApellido.getText(), txtCedula.getText(), 0, 0,0,0, "Sin presentar"); 		
				seccion.inscribirAlumno(alumno);			
			}else 
			{
				JOptionPane.showMessageDialog(null, "Este alumno ya esta inscrito", "Sistema", JOptionPane.INFORMATION_MESSAGE);
				
			    txtCedula.setText("");
			    txtNombre.setText("");
			    txtApellido.setText("");
			}	
		}
	
	}
	
	
	public VistaAlumno(Seccion seccion) {
		super();
		initGUI(seccion);
	}
	
	private void initGUI(final Seccion seccion) {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setVisible(true);
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				jPanel1.setLayout(null);
				jPanel1.setBackground(new java.awt.Color(255,255,255));
				{
					jLabel1 = new JLabel();
					jPanel1.add(jLabel1);
					jLabel1.setText("Cedula: ");
					jLabel1.setBounds(31, 35, 72, 14);
				}
				{
					jLabel2 = new JLabel();
					jPanel1.add(jLabel2);
					jLabel2.setText("Nombre: ");
					jLabel2.setBounds(31, 76, 72, 14);
				}
				{
					jLabel3 = new JLabel();
					jPanel1.add(jLabel3);
					jLabel3.setText("Apellido: ");
					jLabel3.setBounds(31, 113, 72, 14);
				}
				{
					txtCedula = new JTextField();
					jPanel1.add(txtCedula);
					txtCedula.setBounds(110, 32, 105, 21);
				}
				{
					txtNombre = new JTextField();
					jPanel1.add(txtNombre);
					txtNombre.setBounds(110, 73, 142, 21);
				}
				{
					txtApellido = new JTextField();
					jPanel1.add(txtApellido);
					txtApellido.setBounds(110, 110, 142, 21);
				}
				{
					btnRegistrar = new JButton();
					jPanel1.add(btnRegistrar);
					btnRegistrar.setText("Registrar");
					btnRegistrar.setBounds(277, 32, 102, 21);
					btnRegistrar.setBackground(new java.awt.Color(173,216,230));
				}
				{
					btnBuscar = new JButton();
					jPanel1.add(btnBuscar);
					btnBuscar.setText("...");
					btnBuscar.setBounds(227, 32, 26, 21);
					btnBuscar.setBackground(new java.awt.Color(173,216,230));
				}
				{
					btnLimpiar = new JButton();
					jPanel1.add(btnLimpiar);
					btnLimpiar.setText("Limpiar");
					btnLimpiar.setBounds(277, 73, 102, 21);
					btnLimpiar.setBackground(new java.awt.Color(173,216,230));
				}
				{
					btnRetirar = new JButton();
					jPanel1.add(btnRetirar);
					btnRetirar.setText("Retirar");
					btnRetirar.setBounds(277, 110, 102, 21);
					btnRetirar.setBackground(new java.awt.Color(173,216,230));
				}

			}
			//final Seccion seccion= new Seccion();
			
			btnRegistrar.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent evt) {
										
					try {
						
						datosAlumno(seccion);
						txtCedula.setText("");
						txtNombre.setText("");
						txtApellido.setText("");
									
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
				}
			});
			
			btnLimpiar.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent evt)
				{	
					txtCedula.setText("");
					txtNombre.setText("");
					txtApellido.setText("");
				}
				
				
			});
			
			btnRetirar.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent evt)
				{	
					try {
						seccion.retirarAlumno(txtCedula.getText());
						txtCedula.setText("");
						txtNombre.setText("");
						txtApellido.setText("");
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
			});

			btnBuscar.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent evt)
				{	
					
					if (existeElem(txtCedula.getText(),seccion)==true)
					{
						int i=buscarInd(txtCedula.getText(),seccion);
						txtCedula.setText(seccion.getAlumno().elementAt(i).getCedula());
						txtNombre.setText(seccion.getAlumno().elementAt(i).getNombre());
						txtApellido.setText(seccion.getAlumno().elementAt(i).getApellido());
						
					}
					else JOptionPane.showMessageDialog(null, "El alumno no se encuentra inscrito", "Sistema", JOptionPane.INFORMATION_MESSAGE);
				}
				
				
			});
			this.setTitle("Alumnos");
			this.setLocationRelativeTo(null);
			this.setVisible(true);
			pack();
			setSize(420, 220);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	public static boolean existeElem(String cedula, Seccion seccion)
	{
		boolean existe = false;			
		
		for (int i=0;i<seccion.getAlumno().size();i++)
		{
						
			if (seccion.getAlumno().elementAt(i).getCedula().equals(cedula))
			{				
				existe=true;
			}			
		}		
		return existe;
	}
	
	
	public static int buscarInd(String cedula, Seccion seccion)
	{
		int indice=0;
		
			for (int i=0;i<seccion.getAlumno().size();i++)
			{
				if (seccion.getAlumno().elementAt(i).getCedula().equals(cedula))
				{
					indice=i;
				}
				
			}
		return indice;
	}

}
